<?php
  require_once('./db.php');
  require_once('./header.php');

  $file = $_SERVER['PHP_SELF'];
  $id = isset($_GET['id'])?$_GET['id']:0;
  $msg = '';
  $_GET = arrayCleanup($_GET);

  ////////  Sorting //////////////
  if(isset($_GET['order']) && !empty($_GET['order']) && isset($_GET['field']) && !empty($_GET['field'])) {

    $order = $_GET['order'];
    $field = $_GET['field'];

    $orderBy = "$field $order";
  } else {
    $orderBy = "`added_date` DESC";
  }

  $newOrder =  isset($_GET['order']) && $_GET['order']=='asc'?'desc':'asc';

  ////////////////////////////////

  ////////  Search //////////////
  if(isset($_GET['search']) && !empty($_GET['search'])) {

    $search = $_GET['search'];
    $where = "`name` LIKE '%$search%' OR `email` LIKE '%$search%' ";
  } else {
    $where = "";
    $search = "";
  }
  ////////////////////////////////

  //?status=0&mode=status&id=5
  if(isset($_GET['mode']) &&
  $_GET['mode']=='status') {
    $newStatus = $_GET['status']==1?0:1;
    $updateData =  array('status'=>$newStatus);
    $affectedRows = update('users',"`id`='$id'",$updateData);

    if($affectedRows == 1){
      $msg = 'Status updated  successfully';
    }
  }
  if(isset($_GET['mode']) &&
  $_GET['mode']=='del') {

    $deletedRows = delete('users', "`id`='$id'");
    if($deletedRows == 1){
      $msg = "Record Deleted Successfully";
    }
  }

  # Calculate Total Number of USers

  $totalUser = countAll('users',$where);
  $rpp = 5;# record per page
  $totalPages = ceil($totalUser / $rpp);
  $curPage = isset($_GET['page']) ? $_GET['page'] : 1;
  $limit = ($curPage - 1) * $rpp;

  $result = fetchAll('users',$where,'',$orderBy,"$limit, $rpp");

?>

<h1 class="page-header">All Users</h1>

<?php
if($msg !='') {
?>
<div class="alert alert-dismissable alert-success">
              <button data-dismiss="alert" class="close" type="button">×</button>
              <?=$msg?>.
            </div>
<?php
}
?>

<div style="  float: left;margin-bottom: 31px;width: 100%;">
  <div style="float:left;">Total Users <span class="badge"><?=$totalUser?></span></div>

<form action="" class="navbar-form navbar-right" method="get">
  <div style="float:right"><input name="search" type="text" placeholder="Search..." class="form-control"> &nbsp;<button type="submit" class="btn btn-primary" value="Search"><span class="glyphicon glyphicon-search"></span></button></div>
</form>

</div>
<hr>
<a href="export.php">Export Users</a>
<hr>
<table class="table table-bordered table-hover" >
  <thead>
    <tr>
      <th align="left">#</th>
      <th align="left">
<a href="display_users.php?field=name&order=<?=$newOrder?>">Name <span class="glyphicon glyphicon-sort"></span>
</a>
      </th>
      <th align="left">Email</th>
      <th align="left">Avatar</th>
      <th align="left">Added date</th>
      <th align="left">Status</th>
      <th align="left">Action</th>
    </tr>
  </thead>
  <tbody>
<?php
  while($ans = mysql_fetch_assoc($result))
  {
    $id = $ans['id'];
    $status = $ans['status'];
    $imagePath = "uploads/".$ans['avatar'];
?>
    <tr>
      <td><?=$id?></td>
      <td><?=$ans['name']?></td>
      <td>

        <a href="mailto:<?=$ans['email']?>"><?=$ans['email']?></a>
      </td>
      <td><img height="90" width="120" src="<?=$imagePath?>"></td>

      <td><?=date('d-M-Y G:i a',strtotime($ans['added_date']))?></td>

      <td>
        <a href="<?=$file?>?status=<?=$status?>&mode=status&id=<?=$id?>">
        <?=getStatus($status)?>
        </a>
      </td>

      <td>

      <a target="_blank" href="edit_user.php?id=<?=$id?>"><span class="glyphicon glyphicon-edit"></span></a>
      <a onClick="return confirm('Are you sure ?');" href="display_users.php?id=<?=$id?>&mode=del"><span class="glyphicon glyphicon-trash"></span></a>

      </td>
    </tr>
<?php
  }// end while
?>

  </tbody>
</table>


<ul class="pagination pagination-lg">

<?php
  for($i=1 ; $i<=$totalPages ;$i++)
  {
    if($curPage == $i){
      $class = 'active';
    } else {
      $class = '';
    }
    echo "<li class=\"$class\"><a href='$file?page=$i'>$i</a></li>";
  }
?>
</ul>

<?php
  require_once("footer.php");
?>
